package com.mail;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import org.apache.poi.ss.formula.functions.Address;

public class MailUtil {
	private Properties properties=new Properties();
	private Session session=null;
	private Message message=null;
	{
		properties.setProperty("mail.debug", "");
		properties.setProperty("mail.stmp.auth", "true");
		properties.setProperty("mail.transport.protocol", "smtp");
		session=Session.getInstance(properties);
		session.setDebug(true);
		message=new MimeMessage(session);
	}
	public Message getMessage(){
		return message;
	}
	public Session getSession(){
		return session;
	}
	public Transport getTransport(Session session) throws NoSuchProviderException{
		return session.getTransport();
	}
	public void sendEmail() throws MessagingException, UnsupportedEncodingException{
		Session session=getSession();
		Transport transport=getTransport(session);
		transport.connect("smtp.126.com",25, "sywh1234@126.com", "@#uxybddip125");
		Message msg=getMessage();
		Multipart multipart=new MimeMultipart();
		msg.setContent(multipart);
		msg.setContent("<font color='red'>Hello,this is my first Email case</font>","text/html;charset=utf-8");
		InternetAddress address=new InternetAddress("sywh1234@163.com");
		msg.setFrom(new InternetAddress("\""+MimeUtility.encodeText("哈哈这谁呀")+"\"<sywh1234@126.com>"));
		msg.setReplyTo(new javax.mail.Address[]{new InternetAddress("\""+MimeUtility.encodeText("这又是谁啊")+"\"<kai.zhu@cltr.org>")});
		msg.setRecipients(RecipientType.TO, InternetAddress.parse("sywh1234@126.com,kai.zhu@cltr.org"));
		//Transport.send(msg);
		//transport.send(msg);
		transport.sendMessage(msg, new javax.mail.Address[]{address});
		
	}
	public static void main(String[] args) {
		MailUtil mailUtil=new MailUtil();
		try {
			mailUtil.sendEmail();
		} catch (MessagingException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

}
